package cn.dankal.flyaudio.personal.brand

import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder
import cn.dankal.basiclib.util.image.PicUtils
import cn.dankal.slidebarlib.BrandBean
import cn.dankal.flyaudio.R
import kotlinx.android.synthetic.main.rv_item_brand.view.*

class BrandAdapter(val mLayoutManager: LinearLayoutManager) : BaseRecyclerViewAdapter<BrandBean>() {

    override fun getLayoutResId(viewType: Int): Int {
        return R.layout.rv_item_brand
    }

    override fun getViewHolder(parent: ViewGroup?, rootView: View?, viewType: Int): BaseRecyclerViewHolder<*> {
        return BrandAdapter.ViewHolder(rootView)
    }




    class ViewHolder(itemView: View?) : BaseRecyclerViewHolder<BrandBean>(itemView) {
        override fun onBindData(data: BrandBean, position: Int) {
            PicUtils.loadAvatar(data.avatar,itemView.iv_brand)
            itemView.tv_brand.text=data.brandName
        }

    }

    /**
     * 滚动RecyclerView到索引位置
     *
     * @param index
     */
    fun scrollToSection(index: String) {
        if (datas == null || datas.isEmpty()) return
        if (TextUtils.isEmpty(index)) return
        val size = datas.size
        for (i in 0 until size) {
            if (TextUtils.equals(index.substring(0, 1), datas.get(i).getSection().substring(0, 1))) {
                if (mLayoutManager != null) {
                    mLayoutManager.scrollToPositionWithOffset(i, 0)
                }
            }
        }
    }
}